﻿import meps.net.CLocalBridge;

class Main {
	
	public static function main(swfRoot:MovieClip):Void {
		var br:Main = new Main();
	}
	
	private var _text1:TextField;
	private var _text2:TextField;
	private var _text3:TextField;
	private var _bridge1:CLocalBridge;
	private var _bridge2:CLocalBridge;
	private var _bridge3:CLocalBridge;
	private var _receiver1:Object;
	private var _receiver2:Object;
	private var _receiver3:Object;
	
	public function Main() {
		var _this:Object = this;
		_receiver1 = {
			onOne:onOneListener1,
			onTwo:onTwoListener1,
			onThree:onThreeListener1
		};
		_receiver2 = {
			onOne:onOneListener2,
			onTwo:onTwoListener2,
			onThree:onThreeListener2
		};
		_receiver3 = {
			onOne:onOneListener3,
			onTwo:onTwoListener3,
			onThree:onThreeListener3
		};
		
		var width:Number = Stage.width;
		var height:Number = Stage.height / 3;
		
		_text1 = _root.createTextField("text1", _root.getNextHighestDepth(), 0, 0, width, height);
		_text1.background = true;
		_text1.backgroundColor = 0xDDFFDD;
		
		_text2 = _root.createTextField("text2", _root.getNextHighestDepth(), 0, height, width, height);
		_text2.background = true;
		_text2.backgroundColor = 0xDDDDFF;
		
		_text3 = _root.createTextField("text3", _root.getNextHighestDepth(), 0, height * 2, width, height);
		_text3.background = true;
		_text3.backgroundColor = 0xFFDDDD;
		
		log("init");
		
		_bridge1 = new CLocalBridge(_receiver1, this);
		_bridge1.onConnect = onConnect;
		_bridge1.onCall = onCall;
		_bridge1.connect("localbridge.loopback");
		
		_bridge2 = new CLocalBridge(_receiver2, this);
		_bridge2.onConnect = onConnect;
		_bridge2.onCall = onCall;
		_bridge2.connect("localbridge.loopback");
		
		_bridge3 = new CLocalBridge(_receiver3, this);
		_bridge3.onConnect = onConnect;
		_bridge3.onCall = onCall;
		_bridge3.connect("localbridge.between");
		
		_bridge1.call("onOne", "2 from bridge one");
		_bridge2.call("onOne", "2 from second bridge");
		_bridge3.call("onOne", "2 from AS2");
		_bridge1.call("onTwo", 2234.5678, { a:"a", b:2, c:true }, ["a", 2, true]);
		_bridge3.call("onTwo", 2333.3333, { x:2, y:{}, z:3 }, [true, 2, "c"]);
		_bridge2.call("onTwo", 2678.1234, { x:false, y:[2, 2, 3], z:"0" }, [2, "b", false]);
		_bridge1.call("onThree", "a", 2, true);
		_bridge2.call("onThree", 2, "b", false);
		_bridge3.call("onThree", true, 2, "c");
		_bridge1.call("onNone");
		_bridge2.call("onNone", "boo");
		_bridge3.call("onNone", 2234);
	}
	
	public function log(text:String, num:Number):Void {
		var time:String = String(getTimer());
		while (time.length < 4)
			time = "0" + time;
		time =  time.substr(0, time.length - 3) + "." + time.substr(-3);
		if (num == 1) {
			_text1.text += time + ": " + text + "\n";
			trace("1MN:" + text);
		} else if (num == 2) {
			_text2.text += time + ": " + text + "\n";
			trace("2MN:" + text);
		} else if (num == 3) {
			_text3.text += time + ": " + text + "\n";
			trace("3MN:" + text);
		} else {
			_text1.text += time + ": " + text + "\n";
			_text2.text += time + ": " + text + "\n";
			_text3.text += time + ": " + text + "\n";
			trace("MN:" + text);
		}
		_text1.scroll = _text1.maxscroll;
		_text2.scroll = _text2.maxscroll;
		_text3.scroll = _text3.maxscroll;
	}
	
	private function onConnect(target:CLocalBridge):Void {
		//log("onConnect", target == _bridge1 ? 1 : target == _bridge2 ? 2 : 3);
	}
	
	private function onCall(target:CLocalBridge):Void {
		//log("onCall", target == _bridge1 ? 1 : target == _bridge2 ? 2 : 3);
	}
	
	private function onOneListener1(param:String):Void {
		log("onOneListener param:" + param, 1);
	}
	
	private function onTwoListener1(par1:Number, par2:Object, par3:Array):Void {
		log("onTwoListener par1:" + par1 + ", par2:" + strObj(par2) + ", par3:" + par3, 1);
	}
	
	private function onThreeListener1(args:Array):Void {
		log("onThreeListener args:" + args, 1);
	}
	
	private function onOneListener2(param:String):Void {
		log("onOneListener param:" + param, 2);
	}
	
	private function onTwoListener2(par1:Number, par2:Object, par3:Array):Void {
		log("onTwoListener par1:" + par1 + ", par2:" + strObj(par2) + ", par3:" + par3, 2);
	}
	
	private function onThreeListener2(args:Array):Void {
		log("onThreeListener args:" + args, 2);
	}
	
	private function onOneListener3(param:String):Void {
		log("onOneListener param:" + param, 3);
	}
	
	private function onTwoListener3(par1:Number, par2:Object, par3:Array):Void {
		log("onTwoListener par1:" + par1 + ", par2:" + strObj(par2) + ", par3:" + par3, 3);
	}
	
	private function onThreeListener3(args:Array):Void {
		log("onThreeListener args:" + args, 3);
	}
	
	private function strObj(o:Object):String {
		var s:String = "";
		for (var i:String in o)
			s += (s.length > 0 ? ", " : " ") + i + ":" + o[i];
		return "{" + s + " }";
	}
	
}
